<template>
  <div class="app-container">
    <div class="search-container">
      <div class="search-wrapper">
        <el-button type="primary" @click="add()">添加</el-button>
        <div class="debenture-search">
          <el-autocomplete
            v-model="debentureCode"
            :fetch-suggestions="querySearchAsync"
            class="search-input"
            prefix-icon="el-icon-search"
            placeholder="点击输入证券名称"
            @select="handleSelect"
          />
          <el-button type="primary" @click="search()">搜索</el-button>
        </div>
      </div>
    </div>
    <el-table
      :row-class-name="tableRowClassName"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="debentureCode"
        label="债券代码"
        width="180"/>
      <el-table-column
        prop="debentureName"
        label="债券名称"
        width="180"/>
      <el-table-column
        prop="market"
        label="市场(CNBD)"/>
      <el-table-column
        prop="parValue"
        label="面值"/>
      <el-table-column
        prop="valueDate"
        label="起息日"/>
      <el-table-column
        prop="dueDate"
        label="到息日"/>
      <el-table-column
        prop="fixedInterestRate"
        label="固定利率"/>
      <el-table-column
        prop="address"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row.id)" >编辑</el-button>
          <el-button type="text" size="small" @click="deleteOne(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="page"
      :total="total"
      :page-size="limit"
      class="pagination"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"/>
  </div>
</template>

<script>
import instrument from '@/api/instrument'
export default {
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      tableData: [],
      debentureCode: '',
      page: 1, // 当前页
      limit: 10, // 每页个数
      total: 1 // 总页码
    }
  },
  created() {
    this.selectPage()
  },
  methods: {
    add() {
      this.$router.push({ path: '/table/add' })
    },
    search() {
      instrument.getDebentureByCode(this.page, this.limit, this.debentureCode).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
      })
    },
    selectPage() {
      instrument.page(this.page, this.limit).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
      })
        .catch(error => { // 请求失败
          console.log(error)
        })
    },
    handleCurrentChange(val){
      this.page = val;
      this.selectPage();
    },
    onSubmit() {
      this.$message('submit!')
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    handleClick(id) {
      // window.location.href = '/table/find/' + id
      this.$router.push({ path: '/table/find/' + id })
    },
    deleteOne(id) {
      instrument.deleteOne(id).then(response => {
        this.selectPage()
      })
    }
  }
}
</script>

<style scoped>
.search-input{
  margin-left: 900px;
}
.search_button{
  background: #409EFF;
  height: 50px;
  width: 160px;
  color: white;
  left: 20px;
}
</style>

